package com.hgvip.controller;

import com.hgvip.entity.User;
import com.hgvip.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by arnold.zhu on 6/8/2017.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public Object getUserInfo(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("testKey");
        return user;
    }

    @RequestMapping(value = "/setUserInfo", method = RequestMethod.GET)
    public Object setUserInfo(User user, HttpServletRequest request) {
        request.getSession().setAttribute("testKey", user);
        return userService.getUserInfo(user);
    }

    /***
     * 上面两个方法用于测试分布式Session共享,下面两个方法用于测试分布式缓存
     */

    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public Object addUser(User user, HttpServletRequest request) {
        return userService.addUser(user);
    }

    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public List<User> getUsers() {
        return userService.getUsers();
    }

}
